Skip to content

Conversation

boondocklabs
Copy link
Contributor

  • Adds RngExt trait to constrain and enable RNG peripheral with HSI48 clock
  • Implements state machine with Rng<Stopped> and Rng<Running> types
  • Provides blocking and non-blocking read methods for 32-bit random values
  • Implements TryRngCore from rand for interoperability with rand traits
  • Includes error handling for seed and clock error conditions

- Adds `RngExt` trait to constrain and enable RNG peripheral with HSI48 clock
- Implements state machine with `Rng<Stopped>` and `Rng<Running>` types
- Provides blocking and non-blocking read methods for 32-bit random values
- Implements `TryRngCore` from `rand` for interoperability with rand traits
- Includes error handling for seed and clock error conditions
use crate::{rcc::Rcc, stm32::RNG};
use core::{fmt::Formatter, marker::PhantomData};

pub enum RngError {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind implementing defmt::Format when the defmt feature is enabled?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a conditional impl of defmt::Format

let status = unsafe { (*RNG::ptr()).sr().read() };

// Check if the seed or clock error bits are set
if status.seis().bit_is_set() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not know about G4, but I believe for the H5 you have to reset the bit to ensure the peripheral is restored to a valid state. See stm32-rs/stm32h5xx-hal#37

Copy link
Contributor Author

@boondocklabs boondocklabs Aug 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a seed_error_recovery() method that performs the recovery sequence from a seed error. The clock error resets itself when the clock condition (fRNGCLOCK > fHCLK/32) is satisfied per RM0440.

This requires the caller calls seed_error_recovery() if a read returns RngError::SeedError. I considered making reads self-recover - but I figured in some cases it would be useful to know there was a seed error so I've left it explicit.

Cargo.toml Outdated
@@ -20,6 +20,7 @@ stm32-usbd = { version = "0.7.0", optional = true }
fixed = { version = "1.28.0", optional = true }
embedded-io = "0.6"
stm32-hrtim = { version = "0.1.0", optional = true }
rand = { version = "0.9", default-features = false }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use rand_core instead of rand?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed to rand_core in dependencies, and moved rand to dev-dependencies which is required for the example.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! Thanks

Copy link
Member

@usbalbin usbalbin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, looks good to me!

@usbalbin usbalbin added this pull request to the merge queue Aug 13, 2025
Merged via the queue into stm32-rs:main with commit c38187d Aug 13, 2025
34 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants